package com.smartboot.plus.condition;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class ConditionGroup implements Cloneable , Serializable {
    public static final String AND = "AND";
    public static final String OR = "OR";

    private List<ConditionQ> conditionQS = new ArrayList<>();
    private String logicalOperator = AND;
    private int limit = -1;     // 默认值 -1 表示未设置
    private int offset = -1;    // 默认值 -1 表示未设置
    private List<Order> orders = new ArrayList<>();
    private final List<String> orderByList = new ArrayList<>();
    private boolean isCount = false; // 新增标志位

    public ConditionGroup setCount(boolean isCount) {
        this.isCount = isCount;
        return this;
    }

    public ConditionGroup() {

    }

    public ConditionGroup(String logicalOperator) {
        this.logicalOperator = logicalOperator;
    }

    public ConditionGroup order(String column, String direction) {
        orders.add(new Order(column, direction));
        return this;
    }

    public ConditionGroup add(ConditionQ conditionQ) {
        conditionQS.add(conditionQ);
        return this;
    }


    public ConditionGroup offset(int offset) {
        this.offset = offset;
        return this;
    }

    public ConditionGroup limit(int size) {
        this.limit = size;
        return this;
    }

    public ConditionGroup add(ConditionGroup condition) {
        condition.conditionQS.forEach(s -> {
            if (s.getValue() != null && !s.getValue().equals("")) {
                conditionQS.add(s);
            }
        });

        return this;
    }

    public <T> ConditionGroup order(SFunction<T, ?> columnLambda, String direction) {
        String column = LambdaCondition.resolveFieldName(columnLambda);
        direction = (direction == null || direction.isEmpty()) ? "ASC" : direction.toUpperCase();
        orderByList.add(column + " " + direction);
        return this;
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        // 过滤合法条件
        List<String> validConditions = conditionQS.stream()
                .filter(q -> q != null && q.getColumn() != null && !q.getColumn().trim().isEmpty())
                .map(ConditionQ::toString)
                .collect(Collectors.toList());

        // 拼接 WHERE 部分
        if (validConditions.isEmpty()) {
            sb.append(" 1=1 ");
        } else {
            sb.append(" (").append(String.join(" " + logicalOperator + " ", validConditions)).append(") ");
        }

        if (!isCount) {
            appendOrderBy(sb);
            appendLimitOffset(sb);
        }

        return sb.toString();
    }

    // 拼接 ORDER BY
    private void appendOrderBy(StringBuilder sb) {
        if (!orders.isEmpty()) {
            sb.append(" ORDER BY ");
            for (int i = 0; i < orders.size(); i++) {
                if (i > 0) sb.append(", ");
                sb.append(orders.get(i).toSql());
            }
        }
    }

    // 拼接 LIMIT OFFSET
    private void appendLimitOffset(StringBuilder sb) {
        if (offset != -1) {
            sb.append(" OFFSET ").append(offset);
        }
        if (limit != -1) {
            sb.append(" LIMIT ").append(limit);
        }
    }




    // 内部类：表示排序条件
    public static class Order {
        private String column;
        private String direction;

        public Order(String column, String direction) {
            this.column = column;
            this.direction = direction;
        }

        public String toSql() {
            return column + " " + direction.toUpperCase();
        }
    }

}